package com.tms.basic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tms.basic.pojo.domain.Driver;
import com.tms.basic.pojo.req.DriverQueryReq;
import com.tms.basic.pojo.vo.DriverVO;
import com.tms.basic.service.DriverService;
import com.tms.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 驾驶员信息管理接口
 *
 * @author TMS
 * @since 2024-01-01
 */
@Tag(name = "驾驶员管理", description = "驾驶员信息的增删改查接口")
@RestController
@RequestMapping("/driver")
@RequiredArgsConstructor
public class DriverController {

    private final DriverService driverService;

    @Operation(summary = "分页查询驾驶员信息", description = "根据条件分页查询驾驶员信息")
    @PostMapping("/page")
    public R<IPage<DriverVO>> getDriverPage(@RequestBody DriverQueryReq req) {
        return driverService.getDriverPage(req);
    }

    @Operation(summary = "查询驾驶员列表", description = "根据条件查询驾驶员信息列表")
    @PostMapping("/list")
    public R<List<DriverVO>> getDriverList(@RequestBody DriverQueryReq req) {
        return driverService.getDriverList(req);
    }

    @Operation(summary = "根据ID查询驾驶员详情", description = "根据驾驶员ID查询驾驶员详细信息（包含驾驶证）")
    @GetMapping("/{id}")
    public R<DriverVO> getDriverById(@Parameter(description = "驾驶员ID") @PathVariable Long id) {
        return driverService.getDriverById(id);
    }

    @Operation(summary = "根据手机号查询驾驶员", description = "根据手机号查询驾驶员信息")
    @GetMapping("/phone/{phoneNumber}")
    public R<DriverVO> getDriverByPhoneNumber(@Parameter(description = "手机号") @PathVariable String phoneNumber) {
        return driverService.getDriverByPhoneNumber(phoneNumber);
    }

    @Operation(summary = "根据身份证号查询驾驶员", description = "根据身份证号查询驾驶员信息")
    @GetMapping("/id-card/{idCardNumber}")
    public R<DriverVO> getDriverByIdCardNumber(@Parameter(description = "身份证号") @PathVariable String idCardNumber) {
        return driverService.getDriverByIdCardNumber(idCardNumber);
    }

    @Operation(summary = "新增驾驶员", description = "新增驾驶员信息")
    @PostMapping
    public R<Boolean> addDriver(@Valid @RequestBody Driver driver) {
        return driverService.addDriver(driver);
    }

    @Operation(summary = "修改驾驶员信息", description = "修改驾驶员信息")
    @PutMapping
    public R<Boolean> updateDriver(@Valid @RequestBody Driver driver) {
        return driverService.updateDriver(driver);
    }

    @Operation(summary = "删除驾驶员", description = "根据ID删除驾驶员信息")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteDriver(@Parameter(description = "驾驶员ID") @PathVariable Long id) {
        return driverService.deleteDriver(id);
    }

    @Operation(summary = "批量删除驾驶员", description = "根据ID列表批量删除驾驶员信息")
    @DeleteMapping("/batch")
    public R<Boolean> deleteDriverBatch(@RequestBody List<Long> ids) {
        return driverService.deleteDriverBatch(ids);
    }

    @Operation(summary = "更新驾驶员状态", description = "更新驾驶员的状态信息")
    @PutMapping("/{id}/status/{status}")
    public R<Boolean> updateDriverStatus(
            @Parameter(description = "驾驶员ID") @PathVariable Long id,
            @Parameter(description = "新状态") @PathVariable Integer status) {
        return driverService.updateDriverStatus(id, status);
    }

    @Operation(summary = "查询驾照即将到期的驾驶员", description = "查询驾照即将到期的驾驶员")
    @GetMapping("/license/expiring")
    public R<List<DriverVO>> getLicenseExpiringDrivers(
            @Parameter(description = "提前天数，默认30天") @RequestParam(defaultValue = "30") Integer days) {
        return driverService.getLicenseExpiringDrivers(days);
    }

    @Operation(summary = "驾驶员状态统计", description = "根据状态统计驾驶员数量")
    @GetMapping("/status/count")
    public R<List<Map<String, Object>>> countDriverByStatus(
            @Parameter(description = "机构ID") @RequestParam(required = false) Long organizationId) {
        return driverService.countDriverByStatus(organizationId);
    }

    @Operation(summary = "查询空闲驾驶员", description = "查询空闲的驾驶员列表")
    @GetMapping("/available")
    public R<List<DriverVO>> getAvailableDrivers(
            @Parameter(description = "驾照类型") @RequestParam(required = false) String licenseType,
            @Parameter(description = "机构ID") @RequestParam(required = false) Long organizationId) {
        return driverService.getAvailableDrivers(licenseType, organizationId);
    }
} 